<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>{{ isLogin ? "欢迎回来" : "创建账号" }}</h2>
        <p class="subtitle">
          {{ isLogin ? "登录您的图库账号" : "开始您的图库之旅" }}
        </p>
      </div>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        class="login-form"
        @submit.prevent="handleSubmit"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="账号"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <div class="form-actions">
          <el-button
            type="primary"
            class="submit-btn"
            :loading="loading"
            @click="handleSubmit"
          >
            {{ isLogin ? "登录" : "注册" }}
          </el-button>
        </div>

        <div class="form-footer">
          <span class="switch-mode" @click="toggleMode">
            {{ isLogin ? "没有账号？立即注册" : "已有账号？立即登录" }}
          </span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { Lock, User } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { auth } from "../services/api";

const router = useRouter();
const formRef = ref<FormInstance>();
const loading = ref(false);
const isLogin = ref(true);

const form = ref({
  username: "",
  password: "",
});

const rules = {
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, message: "密码长度不能小于6位", trigger: "blur" },
  ],
};

const toggleMode = () => {
  isLogin.value = !isLogin.value;
  form.value = {
    username: "",
    password: "",
  };
};

const handleSubmit = async () => {
  if (!formRef.value) return;

  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        const response = isLogin.value
          ? await auth.login(form.value.username, form.value.password)
          : await auth.register(form.value.username, form.value.password);

        ElMessage.success(isLogin.value ? "登录成功" : "注册成功");
        // 存储用户信息
        localStorage.setItem("user", JSON.stringify(response.data.user));
        // 跳转到首页
        router.push("/home");
      } catch (error: any) {
        ElMessage.error(error.response?.data?.message || "操作失败");
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 0.5rem;
}

.subtitle {
  color: #909399;
  font-size: 0.9rem;
}

.login-form {
  margin-top: 1.5rem;
}

.form-actions {
  margin-top: 2rem;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
}

.form-footer {
  margin-top: 1rem;
  text-align: center;
}

.switch-mode {
  color: #409eff;
  cursor: pointer;
  font-size: 0.9rem;
}

.switch-mode:hover {
  text-decoration: underline;
}

:deep(.el-input__wrapper) {
  padding: 8px 12px;
}

:deep(.el-input__inner) {
  height: 40px;
}
</style>
